package com.tudou.searchcontrol.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudou.searchcontrol.model.PageInfo;

import com.tudou.searchcontrol.dao.TudouMDao;
import com.tudou.searchcontrol.model.TudouM;
import com.tudou.searchcontrol.service.ITudouMService;

@Service("tudouMService")
public class TudouMService implements ITudouMService {

	@Autowired
	private TudouMDao tudouMDao;
	
	public PageInfo getTudouMByPage(Map searchMap,PageInfo pageInfo){
		return tudouMDao.getObjectPageInfo(searchMap,pageInfo);
	}
	
	public List<TudouM> getTudouMList(Map searchMap){
		return tudouMDao.getObjectList(searchMap);
	}
	
	public int saveOrUpdateTudouM(TudouM tudouM){
		if(tudouM.getId() == null){
			return tudouMDao.addObject(tudouM);
		}else{
			return tudouMDao.updateObject(tudouM);
		}
	}
	
	public TudouM getTudouMById(Long id){
		return tudouMDao.getObjectById(id);
	}
	
	public int deleteTudouM(String ids){
		int delNum = 0;
		if(StringUtils.isNotEmpty(ids)){
			String[] idstr = ids.split(",");
			for(int i=0;i<idstr.length;i++){
				delNum = tudouMDao.deleteObject(Long.valueOf(idstr[i]));
			}
		}
		return delNum;
	}
	
	
	public List<TudouM> getAlbumList(PageInfo pageInfo){
		pageInfo = this.getTudouMByPage(new HashMap(), pageInfo);
		List<TudouM> list  = new ArrayList<TudouM>();
		if(pageInfo != null){
			list = pageInfo.getDataList();
		}
		
		return list;
	}

	
	public List<TudouM> getTudouMList(String url){
		List<TudouM> list = new ArrayList<TudouM>();
		SAXBuilder reader = new SAXBuilder();
		try {
			Document doc = reader.build(url);
			Element root = doc.getRootElement();
			List xmlList= root.getChildren("url");
			for(int i=0;i<xmlList.size();i++){
				TudouM album = new TudouM();
				Element element = (Element)xmlList.get(i);
				Element childElement = element.getChild("data").getChild("display");
				album.setTitle(childElement.getChildText("workName"));
				album.setDirector(childElement.getChildText("director").replace("$$", "/"));
				String introduction = childElement.getChildText("introduction");
				
//				int length = (int)(Math.random()*4)*20+40;
//				if(introduction.length()>length){
//					introduction = introduction.substring(0, length)+"...";
//				}
				
				album.setIntroduction(introduction);
				album.setPirurl(childElement.getChildText("poster"));
				album.setPlayurl(childElement.getChildText("playLink"));
				album.setStarring(childElement.getChildText("starring").replace("$$", "/"));
				album.setType(childElement.getChildText("type").replace("$$", "/"));
				
				album.setRegion(childElement.getChildText("region"));
				album.setLength(childElement.getChildText("length"));
				album.setShowTime(childElement.getChildText("showTime"));
				
				list.add(album);
			}
			
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}
}
